package com.example.script.portal.controller;

import com.example.script.common.api.CommonPage;
import com.example.script.common.api.CommonResult;
import com.example.script.portal.enums.RegisterResult;
import com.example.script.portal.model.dto.ScriptChooseParam;
import com.example.script.portal.model.dto.UserRegisterParam;
import com.example.script.portal.model.vo.ScriptListVo;
import com.example.script.portal.model.vo.UserContentVo;
import com.example.script.portal.service.ProductService;
import com.example.script.portal.service.UserService;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(value = "SysUserController",description = "玩家接口")
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private UserService userService;

    @Autowired
    private ProductService productService;

    @ApiOperation("注册用户")
    @PostMapping("/register")
    public CommonResult<Integer> result(@RequestBody UserRegisterParam param){

        RegisterResult result=userService.registerUser(param);
        if (result==RegisterResult.REG_SUCCESS){
            return CommonResult.success(1, result.getMessage());
        }
        return CommonResult.failed(result.getMessage());
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public CommonResult<Map<String,String>> login(
            @ApiParam("用户名")@RequestParam("username") String username ,
            @ApiParam("密码")@RequestParam("password") String password){
        Map<String, String> login = userService.login(username, password);
        if (!CollectionUtils.isEmpty(login)){
            return CommonResult.success(login,"登录成功");
        }
        return CommonResult.failed("登录失败");
    }

    @ApiOperation("查看用户信息")
    @PostMapping("/showContent")
    public CommonResult<List<UserContentVo>> userContent(String username){
        List<UserContentVo> userContentVoList = userService.userContent(username);
        if (userContentVoList != null && userContentVoList.size() > 0){
            return CommonResult.success(userContentVoList);
        }
        return CommonResult.failed("出现错误请重试");
    }

    @ApiOperation("按名字查找剧本")
    @PostMapping("/findScriptByName")
    public CommonResult<List<ScriptListVo>> findByName(String scriptname){
        List<ScriptListVo> scriptListVoList = productService.findByName(scriptname);
        if (scriptListVoList != null && scriptListVoList.size() > 0){
            return CommonResult.success(scriptListVoList);
        }
        return CommonResult.failed("出现错误");
    }

    @ApiOperation("查看所有剧本")
    @PostMapping("/showScriptAll")
    public CommonResult<CommonPage<ScriptListVo>> list(@ApiParam("页码") @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                       @ApiParam("每页包含的条数") @RequestParam(value = "pageSize",defaultValue = "3") Integer pageSize){
        List<ScriptListVo> scriptListVoList = productService.list(pageNum, pageSize);
        if (scriptListVoList != null && scriptListVoList.size() > 0){
            return CommonResult.success(CommonPage.restPage(scriptListVoList));
        }
        return CommonResult.failed("出现错误");
    }

    @ApiOperation("玩家选择剧本")
    @PostMapping("/chooseScript")
    public CommonResult<Integer> chooseScript(@ApiParam("增加商品类型时填写的信息") @RequestBody ScriptChooseParam param){
        int result = productService.chooseScript(param);
        if (result > 0){
            return CommonResult.success(result, "选择剧本成功");
        }
        return CommonResult.failed("选择失败,请重试");
    }
}
